package jsen.tool.ddd.demo.domain.parking;

import jsen.tool.ddd.adapter.infrastructure.specification.ISpecification;
import jsen.tool.ddd.demo.domain.base.DomainObject;
import jsen.tool.ddd.demo.domain.parking.spec.parking.ParkingStatusSpec;
import jsen.tool.ddd.domain.ValueObject;
import lombok.Builder;
import lombok.Getter;

/**
 * 1：occupy
 * 2：freeze
 *
 * @author jsen
 * @date 2021/10/6
 */
@ValueObject(name = "ParkingStatus", desc = "停车记录状态")
@Getter
@Builder
public class ParkingStatus implements DomainObject {
    private final ISpecification<ParkingStatus> strict = new ParkingStatusSpec();
    private final Integer status;
    private final Long startTime;
    private final Long endTime;

    private boolean isOccupy() {
        return status == 1;
    }

    public ParkingStatus freeze() {
        if (!isOccupy()) {
            throw new UnsupportedOperationException("状态错误");
        }
        return ParkingStatus.builder().status(2).startTime(startTime)
                .endTime(System.currentTimeMillis()).build();
    }
}
